﻿using HPITSSBLL;
using HPITSSModel;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HPITSSDAL
{
   public class TuckDAL
    {
        //public static List<Truck> ShowTuckInfo()
        // {
        //     string sql = "select top 5 * from [Truck] ";
        //     SqlDataReader reader = DBHelper.ExecuteReader(sql);
        //     List<Truck> list = new List<Truck>();
        //     while (reader.Read())
        //     {
        //         Truck t = new Truck
        //         {
        //             TruckID = Convert.ToInt32(reader["TruckID"]),
        //             Number = reader["Number"].ToString(),
        //             BuyDate=Convert.ToDateTime(reader["BuyDate"]),
        //             Type = reader["Type"].ToString(),
        //             Length = reader["Length"].ToString(),
        //             Tonnage= Convert.ToInt32(reader["Tonnage"]),
        //             State= Convert.ToInt32(reader["State"]),
        //             Remark= reader["Remark"].ToString(),
        //             CheckInTime = Convert.ToDateTime(reader["CheckInTime"]),
        //             IsDelete = Convert.ToInt32(reader["IsDelete"]),
        //             AlterTime = Convert.ToDateTime(reader["AlterTime"]),
        //             FK_TeamID = Convert.ToInt32(reader["FK_TeamID"])
        //         };

        //         list.Add(t);
        //     }
        //     reader.Close();
        //     return list;
        // }
        /// <summary>
        /// 车辆信息列表
        /// </summary>
        /// <returns></returns>
        public static List<Truck> TruckList()
        {
            string sqlstr = "select * from Truck where Isdelete=0";
            SqlDataReader read = DBHelper.ExecuteReader(sqlstr, null);
            return TruckViewModel(read);
        }
        public static int DelTruck(string TruckID)
        {
            string sqlstr = "update Truck set IsDelete=1 where TruckID=@TruckID";
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@TruckID",TruckID)
            };
            return DBHelper.ExecuteNonQuery(sqlstr, paras);
        }
        /// <summary>
        /// 显示要进行修改的车辆信息
        /// </summary>
        /// <param name="TruckID"></param>
        /// <returns></returns>
        public static Truck EdinTruck(string TruckID)
        {
            string sqlstr = "select * from Truck where TruckID=@TruckID";
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@TruckID",TruckID)
            };
            SqlDataReader read = DBHelper.ExecuteReader(sqlstr, paras);
            List<Truck> list = TruckViewModel(read);
            return list[0];
        }
        public static List<Truck> TruckViewModel(SqlDataReader read)
        {
            List<Truck> truck = new List<Truck>();
            while (read.Read())
            {
                Truck vTruck = new Truck();
                vTruck.TruckID = Convert.ToInt32(read["TruckID"]);
                vTruck.Number = read["Number"].ToString();
                vTruck.BuyDate = Convert.ToDateTime(read["BuyDate"]);
                vTruck.Type = read["Type"].ToString();
                vTruck.Length = read["Length"].ToString();
                vTruck.Tonnage = Convert.ToInt32(read["Tonnage"]);
                vTruck.FK_TeamID = Convert.ToInt32(read["FK_TeamID"]);
                vTruck.State = Convert.ToInt32(read["State"]);
                vTruck.Remark = read["Remark"].ToString();
                vTruck.CheckInTime = Convert.ToDateTime(read["CheckInTime"]);
                vTruck.IsDelete = Convert.ToInt32(read["IsDelete"]);
                vTruck.AlterTime = Convert.ToDateTime(read["AlterTime"]);
                truck.Add(vTruck);
            }
            //关闭连接
            read.Close();
            return truck;
        }
    }
}
